package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);//dishDto是Dish的子类对象，也可以被保存。
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto selectDishDto(long id) {
        DishDto dishDto = new DishDto();
        Dish dish = getById(id);
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);

        List<DishFlavor> flavors = dishFlavorService.list(qw);
        dishDto.setFlavors(flavors);
//        for (DishFlavor flavor : flavors) {
//            List<DishFlavor> list = dishDto.getFlavors();
//            list.add(flavor);
//        }
//        Category category = categoryService.getById(id);
//        String name = category.getName();
//        dishDto.setCategoryName(name);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(qw);//根据dish中的id进行删除
        dishDto.getFlavors().forEach(dishFlavor -> {dishFlavor.setDishId(id);});
        dishFlavorService.saveBatch(dishDto.getFlavors());
        updateById(dishDto);
    }

    @Override
    public Page<DishDto> pageByName(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        qw.like(StringUtils.isNotBlank(name),Dish::getName,name);
        qw.orderByAsc(Dish::getPrice);
        Page<Dish> page1 = this.page(dishPage, qw);
        Page<DishDto> dishDtoPage = new Page<>();//创建了一个dishDtoPage对象
        dishDtoPage.setTotal(page1.getTotal());//将page1的Totals赋值给dishDtoPage
        List<DishDto> records = new ArrayList<>();//创建一个空的List集合
        page1.getRecords().forEach(dish -> {//遍历所有查询到的dish对象
            DishDto dishDto = new DishDto();//创建一个新的dishDto对象
            Category category = categoryService.getById(dish.getCategoryId());
            String categoryName = category.getName();//得到菜品类名
            dishDto.setCategoryName(categoryName);//将所有的菜名给显现出来
            records.add(dishDto);//将dish转换成的dishDto添加到新建的集合中
        });
        dishDtoPage.setRecords(records);//将Page对象存储的老的保存数据的集合对象修改为新的集合对象
        return dishDtoPage;
    }

    @Override
    public void removeByIdsWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("还有正在起售的套餐");
        }
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId,id);
            List<DishFlavor> dishFlavors = dishFlavorService.list(qw);
            for (DishFlavor dishFlavor : dishFlavors) {
                Long flavorId = dishFlavor.getId();
                dishFlavorService.removeById(flavorId);
            }
        }
        this.removeByIds(ids);
    }

    @Override
    public List<DishDto> getDishWithFlavor(Dish dish) {
        Long categoryId = dish.getCategoryId();
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.checkValNotNull(dish.getStatus()),Dish::getStatus,dish.getStatus());
        qw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,categoryId);
        qw.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        List<Dish> list = this.list(qw);
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish1 : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1,dishDto);
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(dishFlavors);
            dishDtos.add(dishDto);
        }
        return dishDtos;
    }

}
